package com.warm.androidstudy.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by Administrator on 2017/6/27 0027.
 */

public class DateUtils {

    static {
        DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        Continuity_DATE_FORMAT = new SimpleDateFormat("yyyyMMddhhmmss", Locale.US);
        DEFAULT_DATE_FORMAT2 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
        DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        DATE_FORMAT_DATE_MM_DD_CHN = new SimpleDateFormat("MM月dd日", Locale.US);
        YearMonth = new SimpleDateFormat("yyyy-MM", Locale.US);
        TIME_FORMAT_DATE = new SimpleDateFormat("HH:mm:ss", Locale.US);
        TIME_FORMAT_DATE2 = new SimpleDateFormat("HH:mm", Locale.US);
        DEFAULT_DATE_FORMAT3 = new SimpleDateFormat("MM-dd HH:mm", Locale.US);
        DEFAULT_DATE_FORMAT4 = new SimpleDateFormat("yyyy年MM月dd日");
        Default_time = "2000-1-1";
    }

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT;
    public static final SimpleDateFormat Continuity_DATE_FORMAT;
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT2;
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT3;
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT4;
    public static final SimpleDateFormat DATE_FORMAT_DATE;
    public static final SimpleDateFormat DATE_FORMAT_DATE_MM_DD_CHN;
    public static final SimpleDateFormat TIME_FORMAT_DATE;
    public static final SimpleDateFormat TIME_FORMAT_DATE2;
    public static final SimpleDateFormat YearMonth;
    public static String Default_time;
    private static long previousTime;

    private DateUtils() {
        throw new AssertionError();
    }

    /**
     * 时间戳转时间
     * 调用此方法输入所要转换的时间戳输入例如（1402733340） --- 输出（"2014-06-14  16:09:00"）
     *
     * @param timeStamp
     * @return
     */
    public static String timeStampConverToTime(long timeStamp) {
        String timeString = "";
        timeString = DEFAULT_DATE_FORMAT.format(new Date(timeStamp));//年月日 - 时分秒
        return timeString;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return
     * @throws ParseException
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        Date date = DEFAULT_DATE_FORMAT.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static String formatYY_MM_DDfString(String ddate) {
        Date date = null;
        try {
            date = DEFAULT_DATE_FORMAT.parse(ddate);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        return DATE_FORMAT_DATE.format(date);
    }

    public static String formatMM_dd_HH(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        return 0L == time ? "" : sdf.format(new Date(time));
    }

    public static String formatDefault(Date date) {
        return DEFAULT_DATE_FORMAT.format(date);
    }

    public static String formatyyyyMMddhhmmss(Date date) {
        return Continuity_DATE_FORMAT.format(date);
    }

    public static String DATE_FORMAT_DATE(Date date) {
        return DATE_FORMAT_DATE.format(date);
    }

    public static String DATE_FORMAT_YearMonth(Date date) {
        return YearMonth.format(date);
    }

    public static String formatDefault2(Date date) {
        return DEFAULT_DATE_FORMAT2.format(date);
    }

    public static String formatDefault3(Date date) {
        return DEFAULT_DATE_FORMAT3.format(date);
    }

    public static String dateFormatDate(Date date) {
        return DATE_FORMAT_DATE.format(date);
    }

    public static String formatDefault3(String str) {
        Date date = stringToDate(str);
        return DATE_FORMAT_DATE.format(date);
    }

    public static String formatDefault(String str) {
        Date date = stringToDate(str);
        return DEFAULT_DATE_FORMAT.format(date);
    }

    /**
     * 2019-02-12 10:24:14 转2019年2月12日
     *
     * @param str
     * @return
     */
    public static String formatDefault4(String str) {
        String dateStr = "";
        try {
            Date date = DEFAULT_DATE_FORMAT.parse(str);//年月日
            dateStr = DEFAULT_DATE_FORMAT4.format(date);
        } catch (Exception var3) {
            var3.printStackTrace();
        }
        return dateStr;
    }

    public static String formatHH_mm_ss(Date date) {
        return TIME_FORMAT_DATE.format(date);
    }

    public static String formatHH_mm(Date date) {
        return TIME_FORMAT_DATE2.format(date);
    }

    //yyyy-MM-dd HH:mm:ss
    public static Date stringToDate(String str) {
        Date date = null;

        try {
            date = DEFAULT_DATE_FORMAT.parse(str);//年月日时分秒
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return date;
    }

    public static Date stringYYYYMMDDToDate(String str) {
        Date date = null;
        try {
            date = DATE_FORMAT_DATE.parse(str);//yyyy-MM-dd
        } catch (Exception var3) {
            var3.printStackTrace();
        }
        return date;
    }

    public static Date getYYYYMMToDate(String str) {
        Date date = null;
        try {
            date = YearMonth.parse(str);//yyyy-MM
        } catch (Exception var3) {
            var3.printStackTrace();
        }
        return date;
    }

    public static String getPlusYearsDateMsg(Date date, int plusUnitCounts) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, plusUnitCounts);//把日期往后增加plusYears年.整数往后推,负数则往前移动
//        calendar.add(calendar.DATE, plusUnitCounts);//把日期往后增加一天.整数往后推,负数往前移动
//        calendar.add(calendar.WEEK_OF_MONTH, plusUnitCounts);//把日期往后增加一个星期.整数往后推,负数往前移动
        Date dateResult = calendar.getTime();//这个时间就是日期加上plusYears的结果
        return DateUtils.DATE_FORMAT_YearMonth(dateResult);
    }

    public static String getPlusMonthDateMsg(Date date, int plusUnitCounts) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, plusUnitCounts);
        Date dateResult = calendar.getTime();//这个时间就是日期加上plusYears的结果
        return DateUtils.DATE_FORMAT_DATE(dateResult);
    }

    public static String getPlusDayDateMsg(Date date, int plusUnitCounts) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DAY_OF_MONTH, plusUnitCounts);
        Date dateResult = calendar.getTime();//这个时间就是日期加上plusYears的结果
        return DateUtils.DATE_FORMAT_DATE(dateResult);
    }

    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(1) == cal2.get(1);
        boolean isSameMonth = isSameYear && cal1.get(2) == cal2.get(2);
        boolean isSameDate = isSameMonth && cal1.get(5) == cal2.get(5);
        return isSameDate;
    }

    public static boolean isCurrentYear(Date date1) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date());
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date1);
        boolean isSameYear = cal1.get(1) == cal2.get(1);
        return isSameYear;
    }

    public static boolean isMoreDate(String strDate1, String strDate2, DateFormat df) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        try {
            c1.setTime(df.parse(strDate1));
            c2.setTime(df.parse(strDate2));
        } catch (ParseException var6) {
            System.err.println("格式不正确");
        }

        int result = c1.compareTo(c2);
        return result == 0 ? false : result >= 0;
    }

    public static boolean diffSmall(String timeOne, String timeTwo, SimpleDateFormat TimeFormat) {
        boolean isSmaller = false;

        try {
            Date startTime = TimeFormat.parse(timeOne);
            Date endTime = TimeFormat.parse(timeTwo);
            isSmaller = startTime.getTime() < endTime.getTime();
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return isSmaller;
    }

    public static Integer compareTwoTimes(String timeOne, String timeTwo, SimpleDateFormat TimeFormat) {
        byte result = 0;

        try {
            Date e = TimeFormat.parse(timeOne);
            Date endTime = TimeFormat.parse(timeTwo);
            if (e.getTime() < endTime.getTime()) {
                result = 1;
            } else if (e.getTime() == endTime.getTime()) {
                result = 0;
            } else {
                result = 2;
            }
        } catch (ParseException var6) {
            var6.printStackTrace();
        }

        return Integer.valueOf(result);
    }

    public static String formatMsgTime(Date date) {
        long dateTime = date.getTime();
        String text;
        if (isSameDay(dateTime)) {
            text = "HH:mm:ss";
        } else if (isYesterday(dateTime)) {
            text = "昨天 HH:mm:ss";
        } else if (isSameYear(dateTime)) {
            text = "M月d日 HH:mm:ss";
        } else {
            text = "yyyy-M-d HH:mm:ss";
        }

        previousTime = dateTime;
        return (new SimpleDateFormat(text, Locale.CHINESE)).format(date);
    }

    public static String dateDiff(String startTime, String endTime, String showType) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long ns = 1000L;
        long diff = stringToDate(startTime).getTime() - stringToDate(endTime).getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long DD = day * nd / nm;
        long HH = hour * nh / nm;
        long min = diff % nd % nh / nm;
        long sec = diff % nd % nh % nm / ns;
        String result = "";
        if (showType.equals("minute")) {
            result = String.valueOf(DD + HH + min);
        } else {
            if (hour != 0L) {
                result = hour + "小时";
            }

            if (min != 0L) {
                result = result + min + "分钟";
            }

            if (sec != 0L) {
                result = result + sec + "秒";
            }
        }

        return result;
    }

    public static String formatTimeHHMM(String str) {
        try {
            Date e = stringToDate(str);
            String text = "yyyy-MM-dd HH:mm";
            return (new SimpleDateFormat(text, Locale.CHINESE)).format(e);
        } catch (Exception var3) {
            return str;
        }
    }

    public static String countTime(String begin, String end) {
        int hour = 0;
        int minute = 0;
        long total_minute = 0L;
        StringBuffer sb = new StringBuffer();

        try {
            LogUtil.e("计算前：", begin + "--" + end);
            Date e = DEFAULT_DATE_FORMAT.parse(begin);
            Date end_date = DEFAULT_DATE_FORMAT.parse(end);
            total_minute = (end_date.getTime() - e.getTime()) / 60000L;
            hour = (int) total_minute / 60;
            minute = (int) total_minute % 60;
        } catch (ParseException var9) {
            LogUtil.e("传入的时间格式不符合规定");
        }

        sb.append(hour).append("小时").append(minute).append("分");
        return sb.toString();
    }

    /**
     * 计算两个时间的间隔分钟数
     *
     * @param begin
     * @param end
     * @param sdf
     * @return
     */
    public static int getMinuteCount(String begin, String end, SimpleDateFormat sdf) {
        int hour = 0;
        int minute = 0;
        long total_minute = 0L;

        try {
            LogUtil.e("计算前：", begin + "--" + end);
            Date e = sdf.parse(begin);
            Date end_date = sdf.parse(end);
            total_minute = (end_date.getTime() - e.getTime()) / 60000L;
            hour = (int) total_minute / 60;
            minute = (int) total_minute % 60;
        } catch (ParseException var9) {
            LogUtil.e("传入的时间格式不符合规定");
        }

        int getMinuteCount = hour * 60 + minute;
        return getMinuteCount;
    }

    public static int getMinuteDif(String begin, String end) {
        int hour = 0;
        int minute = 0;
        long total_minute = 0L;

        try {
            LogUtil.e("计算前：", begin + "--" + end);
            Date e = DEFAULT_DATE_FORMAT2.parse(begin);
            Date end_date = DEFAULT_DATE_FORMAT2.parse(end);
            total_minute = (end_date.getTime() - e.getTime()) / 60000L;
            hour = (int) total_minute / 60;
            minute = (int) total_minute % 60;
        } catch (ParseException var9) {
            LogUtil.e("传入的时间格式不符合规定");
        }

        int getMinuteCount = hour * 60 + minute;
        return getMinuteCount;
    }

    public static String getAfterTime(String current, int addMinute) {
        Date date = null;
        String formatTime = "";

        try {
            date = DEFAULT_DATE_FORMAT.parse(current);
            date.setTime(date.getTime() + (long) (addMinute * 60 * 1000));
            formatTime = DEFAULT_DATE_FORMAT.format(date);
        } catch (Exception var5) {
            LogUtil.e("DateUtils -- getAfterTime方法计算时间出错");
        }

        return formatTime;
    }

    public static String getStringTime(int allMinute) {
        int hour = allMinute / 60;
        int minute = allMinute - hour * 60;
        String getTime = hour + "小时" + minute + "分";
        return getTime;
    }

    public static String getCalculateTime(int allSecond) {
        int hour = allSecond / 3600;
        int minute = (allSecond - hour * 60 * 60) / 60;
        int second = allSecond - hour * 60 * 60 - minute * 60;
        String getTime = "";
        if (hour > 0) {
            getTime = hour + "小时" + minute + "分" + second + "秒";
        } else if (minute > 0) {
            getTime = minute + "分" + second + "秒";
        } else {
            getTime = second + "秒";
        }

        return getTime;
    }

    public static String getAddDate(int addDayNum) {
        Calendar c = Calendar.getInstance();
        c.add(5, addDayNum);
        Date monday = c.getTime();
        return DATE_FORMAT_DATE.format(monday);
    }

    public static String getAddDateyyyyMMDD(int addDayNum) {
        Calendar c = Calendar.getInstance();
        c.add(5, addDayNum);
        Date monday = c.getTime();
        return DATE_FORMAT_DATE.format(monday);
    }

    public static String getAddDateMMDD_CHN(int addDayNum) {
        Calendar c = Calendar.getInstance();
        c.add(5, addDayNum);
        Date monday = c.getTime();
        return DATE_FORMAT_DATE_MM_DD_CHN.format(monday);
    }

    public static String GetDateAdd(long m, SimpleDateFormat sdf) {
        Date date = new Date();
        String n = DEFAULT_DATE_FORMAT.format(date);
        Timestamp time = Timestamp.valueOf(n);
        long l = time.getTime() + 86400L * m * 1000L;
        time.setTime(l);
        return sdf.format(time);
    }

    public static String plusDay(int num, String newDate) {
        String enddate = "";

        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date currdate = e.parse(newDate);
            LogUtil.e("现在的日期是：" + currdate);
            Calendar ca = Calendar.getInstance();
            ca.add(5, num);
            currdate = ca.getTime();
            enddate = e.format(currdate);
            LogUtil.e("增加天数以后的日期：" + enddate);
        } catch (Exception var6) {
            LogUtil.e("DateUtils -- plusDay方法计算时间出错");
        }

        return enddate;
    }

    public static String getAddMonth(int month) {
        Calendar c = Calendar.getInstance();
        c.add(2, month);
        Date monday = c.getTime();
        return DATE_FORMAT_DATE.format(monday);
    }

    public static String getYear(Date date, String addType, int num) {
        // Calendar calendar = new GregorianCalendar();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (addType.equals("DAY")) {
            calendar.add(calendar.DATE, num);//增加一天
        } else if (addType.equals("WEEK")) {
            calendar.add(calendar.WEEK_OF_MONTH, num);//增加一个礼拜
        } else if (addType.equals("MONTH")) {
            calendar.add(calendar.MONTH, num);//增加一天
        } else if (addType.equals("YEAR")) {
            calendar.add(calendar.YEAR, num);//把日期往后增加一年.整数往后推,负数往前移动
        }
        //calendar.add(calendar.WEEK_OF_YEAR,1);//增加一个礼拜
        return DATE_FORMAT_DATE.format(calendar.getTime());
    }

    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It\'s unbelievable!");
        } else {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }

    public static String formatMsgTimeHHMM(Date date) {
        long dateTime = date.getTime();
        String text;
        if (isSameDay(dateTime)) {
            text = "HH:mm";
        } else if (isYesterday(dateTime)) {
            text = "昨天 HH:mm";
        } else if (isSameYear(dateTime)) {
            text = "M月d日 HH:mm";
        } else {
            text = "yyyy-M-d HH:mm";
        }

        previousTime = dateTime;
        return (new SimpleDateFormat(text, Locale.CHINESE)).format(date);
    }

    public static String formatMs(String str) {
        String text = "MM-dd";

        try {
            Date e = (new SimpleDateFormat("yyyy-MM-dd", Locale.US)).parse(str);
            return (new SimpleDateFormat(text, Locale.CHINESE)).format(e);
        } catch (Exception var3) {
            var3.printStackTrace();
            return "";
        }
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔多少天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        LogUtil.e(days + "天");
        return Math.abs(days);
    }

    /**
     * 计算两个相差多少个月
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDiffMonths(Date startDate, Date endDate) {
        Calendar startCalendar = new GregorianCalendar();
        startCalendar.setTime(startDate);
        Calendar endCalendar = new GregorianCalendar();
        endCalendar.setTime(endDate);

        int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
        return diffMonth;
    }

    /**
     * 计算两个相差多少毫秒
     *
     * @param compareTime yyyy-MM-dd HH:mm:ss
     * @param TimeFormat  时间格式
     * @return
     */
    public static long getDiffMillSeconds(String compareTime, SimpleDateFormat TimeFormat) {
        try {
            Date newTime = TimeFormat.parse(compareTime);

            String currentTimeStr = DateUtils.formatDefault(new Date()); //yyyy-MM-dd HH:mm:ss
            Date currentTime = TimeFormat.parse(currentTimeStr);
            long diffMillSeconds = newTime.getTime() - currentTime.getTime();
            return diffMillSeconds;
        } catch (ParseException var6) {
            var6.printStackTrace();
            return -1;
        }
    }

    /**
     * 解析date成显示的消息格式
     *
     * @param context
     * @param date
     * @return
     */
    public static String formatDateTime(Context context, Date date) {
        if (is24HourFormat(context)) {
            //24小时制 大H
            long dateTime = date.getTime();
            String text;
            if (isSameDay(dateTime)) {
                Calendar calendar = GregorianCalendar.getInstance();
                if (inOneMinute(dateTime, calendar.getTimeInMillis())) {
                    return "1分钟内";
                }
                if (inOneHour(dateTime, calendar.getTimeInMillis())) {
                    return String.format("%d分钟内", new Object[]{Long.valueOf(Math.abs(dateTime - calendar.getTimeInMillis()) / 60000L)});
                }
                text = "HH:mm";
            } else if (isYesterday(dateTime)) {
                text = "昨天 HH:mm";
            } else if (isSameYear(dateTime)) {
                text = "M月d日 HH:mm";
            } else {
                text = "yyyy-MM-dd HH:mm";
            }
            return (new SimpleDateFormat(text, Locale.CHINESE)).format(date);
        } else {
            //12小时制度的 小h
            long dateTime = date.getTime();
            String text;
            if (isSameDay(dateTime)) {
                Calendar calendar = GregorianCalendar.getInstance();
                if (inOneMinute(dateTime, calendar.getTimeInMillis())) {
                    return "1分钟内";
                }
                if (inOneHour(dateTime, calendar.getTimeInMillis())) {
                    return String.format("%d分钟前", new Object[]{Long.valueOf(Math.abs(dateTime - calendar.getTimeInMillis()) / 60000L)});
                }
                calendar.setTime(date);
                int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);//获取小时数
                if (hourOfDay > 17) {
                    text = "晚上 hh:mm";
                } else if (hourOfDay >= 0 && hourOfDay <= 6) {
                    text = "凌晨 hh:mm";
                } else if (hourOfDay > 11 && hourOfDay <= 17) {
                    text = "下午 hh:mm";
                } else {
                    text = "上午 hh:mm";
                }
            } else if (isYesterday(dateTime)) {
                text = "昨天 HH:mm";
            } else if (isSameYear(dateTime)) {
                text = "M月d日 HH:mm";
            } else {
                text = "yyyy-MM-dd HH:mm";
            }
            return (new SimpleDateFormat(text, Locale.CHINESE)).format(date);
        }
    }

    public static boolean inOneMinute(long time1, long time2) {
        return Math.abs(time1 - time2) < 60000L;
    }

    public static boolean inFiveMinute(long time1, long time2) {
        return Math.abs(time1 - time2) < 300000L;
    }

    public static boolean inOneHour(long time1, long time2) {
        return Math.abs(time1 - time2) < 3600000L;
    }

    public static boolean isSameDay(long time) {
        long startTime = floorDay(Calendar.getInstance()).getTimeInMillis();
        long endTime = ceilDay(Calendar.getInstance()).getTimeInMillis();
        return time > startTime && time < endTime;
    }

    private static boolean isYesterday(long time) {
        Calendar startCal = floorDay(Calendar.getInstance());
        startCal.add(5, -1);
        long startTime = startCal.getTimeInMillis();
        Calendar endCal = ceilDay(Calendar.getInstance());
        endCal.add(5, -1);
        long endTime = endCal.getTimeInMillis();
        return time > startTime && time < endTime;
    }

    private static boolean isSameYear(long time) {
        Calendar startCal = floorDay(Calendar.getInstance());
        startCal.set(2, 0);
        startCal.set(5, 1);
        return time >= startCal.getTimeInMillis();
    }

    private static Calendar floorDay(Calendar startCal) {
        startCal.set(11, 0);
        startCal.set(12, 0);
        startCal.set(13, 0);
        startCal.set(14, 0);
        return startCal;
    }

    private static Calendar ceilDay(Calendar endCal) {
        endCal.set(11, 23);
        endCal.set(12, 59);
        endCal.set(13, 59);
        endCal.set(14, 999);
        return endCal;
    }

    public static String formatYY_MM_DD(long time) {
        return 0L == time ? "" : DATE_FORMAT_DATE.format(new Date(time));
    }

    /**
     * 通过年份和月份 得到当月的日子
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }

    /**
     * 返回当前月份1号位于周几
     *
     * @param year  年份
     * @param month 月份，传入系统获取的，不需要正常的
     * @return 日：1		一：2		二：3		三：4		四：5		五：6		六：7
     */
    public static int getFirstDayWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取当前日期是星期几(返回字符串类型)
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static String getWeekOfDateStr(Date date) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 获取当前是星期几(返回字符串类型)
     *
     * @param dayOfWeek
     * @return 当前日期是周几
     */
    public static String getDayOfWeekStr(int dayOfWeek) {
        String[] weekDays = {"", "周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        return weekDays[dayOfWeek];
    }

    /**
     * 获取当前日期是星期几
     *
     * @param date
     * @return 当前日期是星期几
     */
    public static int getWeekOfDateInt(Date date) {
        Integer[] weekDays = {0, 1, 2, 3, 4, 5, 6};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 判断是否是24小时制
     *
     * @param context
     * @return
     */
    static boolean is24HourFormat(Context context) {
        ContentResolver cv = context.getContentResolver();
        String strTimeFormat = android.provider.Settings.System.getString(cv, android.provider.Settings.System.TIME_12_24);
        if ("24".equals(strTimeFormat)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 月日时分秒，0-9前补0
     *
     * @param number the number
     * @return the string
     */
    @NonNull
    public static String fillZero(int number) {
        return number < 10 ? "0" + number : "" + number;
    }

    /**
     * 根据秒数转化为时分秒   00:00:00
     *
     * @param time
     * @return
     */
    /**
     * 将毫秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    //计算某个时间加上N天后的时间(time格式：年月日时分秒)
    public static Date getAddNDayResult(String time, int addDay) {
        Calendar cal = Calendar.getInstance();
        Date date = DateUtils.stringToDate(time);
        cal.setTime(date);//设置开始时间
        cal.add(Calendar.DATE, addDay);//增加addDay天
        Date result = cal.getTime();
        return result;
    }

    //计算某个时间加上N天后的时间(time格式：年月日)
    public static Date getAddNDayResultByYMD(String time, int addDay) {
        Calendar cal = Calendar.getInstance();
        Date date = DateUtils.stringYYYYMMDDToDate(time);
        cal.setTime(date);//设置开始时间
        cal.add(Calendar.DATE, addDay);//增加addDay天
        Date result = cal.getTime();
        LogUtil.e(DATE_FORMAT_DATE(result));
        return result;
    }

    //本月第一天
    public static String getThisMonthStartDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.getTime();
        return DATE_FORMAT_DATE.format(cal.getTime());
    }

    //本月最后一天
    public static String getThisMonthEndDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,
                cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return DATE_FORMAT_DATE.format(cal.getTime());
    }

    /**
     * 功能描述：返回小时
     *
     * @param date 日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回分
     *
     * @param date 日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public static String formatTime(long time) {
        String hh = time / 3600 > 9 ? time / 3600 + "" : "0" + time / 3600;
        String mm = (time % 3600) / 60 > 9 ? (time % 3600) / 60 + "" : "0" + (time % 3600) / 60;
        String ss = (time % 3600) % 60 > 9 ? (time % 3600) % 60 + "" : "0" + (time % 3600) % 60;
        return hh+":"+mm+":"+ss;
    }
}

